<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Upload;
use think\Url;
class User extends Controller
{
    //处理跨域请求
    public function __construct(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
    } 
    
    //查询用户是否存在并登录
    public function find(){
       $user_name = $_GET['user_name'];
       $res = Db::name('userinfo')
       -> where('user_name',$user_name)
       -> select();
       
       return json($res);
    }
    
    //注册用户
    public function add(){
        $user_name = $_GET['user_name'];
        $data = ['user_name' => $user_name];
        $res = Db::table('userinfo')
        -> insert($data);
        
        return json($res);
    }
    
    //得到用户图片地址
    public function urlImg(Request $req){
        $src = $req->file("file");
        
        return json($src);
    }
    
    //用户修改头像
    public function updateImg(){
        $userId = $_POST['userId'];
        $src = $_POST['src'];
        $data = ['user_src'=>$src];
        $res = Db::table('userinfo')
        -> where('id',$userId)
        -> update($data);
        
        return json($res);
    }
    
    
    
    //根据登录的id查询用户信息
    public function selectUser(){
        $id = $_GET['id'];
        $res = Db::table('userinfo')
        -> where('id',$id)
        -> select();
    
        return json($res);
    }
    
    //根据用户的id查询我的收藏总数
    public function takecount(){
        $user_id = $_GET['id'];
        $res = count(Db::name('userinfo')
            -> alias('a')
            -> join('takeinfo b','a.id = b.user_id')
            -> where('a.id',$user_id)
            -> select());
    
        return json($res);
    }
    
    //根据用户的id查询我的订阅总条数
    public function collectcount(){
        $user_id = $_GET['id'];
        $res = count(Db::name('userinfo')
            -> alias('a')
            -> join('collectinfo b','a.id = b.user_id')
            -> where('a.id',$user_id)
            -> select());
    
        return json($res);
    }
    
    //根据用户id查询我的收藏的信息
    public function takeselect(){
        $user_id = $_GET['id'];
        $res = Db::name('userinfo')
        -> alias('a')
        -> join('takeinfo b','a.id = b.user_id')
        -> where('a.id',$user_id)
        -> select();
    
        return json($res);
    }
    
    //根据用户的id查询我的订阅条件的信息
    public function collectselect(){
        $user_id = $_GET['id'];
        $res = Db::name('userinfo')
        -> alias('a')
        -> join('collectinfo b','a.id = b.user_id')
        -> where('a.id',$user_id)
        -> select();
    
        return json($res);
    }
    
    //根据用户的id，当前用户订阅条件的id删除我的订阅条件的信息
    public function deleteSubId(){
        $userId = $_GET['userId'];
        $subId = $_GET['subId'];
        $res = Db::table('collectinfo')
        -> where('collect_id',$subId)
        -> where('user_id',$userId)
        -> delete();
        
        return json($res);
    }
    
    //根据用户的id添加订阅条件
    public function addSub(){
        //得到post的信息
        $userId = $_POST['userId'];
        $subBus = $_POST['subBus'];
        $subAdd = $_POST['subAdd'];
        $svval = $_POST['svval'];
        $money = $_POST['money'];
        $data = [
            'collect_add'=>$subAdd,
            'collect_money'=>$money,
            'collect_size'=>$svval,
            'collect_bus'=>$subBus,
            'user_id'=>$userId
        ];
        $res = Db::table('collectinfo')
        -> where('user_id',$userId)
        -> insert($data);
        
        return json($res);
    }
    
    //首页模糊查询
	public function selects(){
		$houetype = $_GET['houetype'];
		$res = Db::table('housetypeinfo')
		->where('type_content','like','%'.$houetype.'%')
		->select();
		
		return json($res);
	}
	
		//短信验证（没完成）
	public function query(){
// 		var code = Math.round(Math.random() * 9) + "" + Math.round(Math.random() * 9) + "" + Math.round(Math.random() * 9) + "" + Math.round(Math.random() * 9) + "" + Math.round(Math.random() * 9) + "" + Math.round(Math.random() * 9);
//	console.log(code)
//			var queryData = querystring.stringify({
//				"mobile": req.body.tel, // 接受短信的用户手机号码
//				"tpl_id": "121031", // 您申请的短信模板ID，根据实际情况修改
//				"tpl_value": "#code#=" + code, // 您设置的模板变量，根据实际情况修改
//				"key": "aa8cbfb7f189f4df92b42432a798ffd0", // 应用APPKEY(应用详细页查询)
//			});
//			var queryUrl = 'http://v.juhe.cn/sms/send?' + queryData;
//			request(queryUrl, function(error, response, body) {
//				if(!error && response.statusCode == 200) {
//					var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容
// 	res.json({
// 			msg: code,
// 			tel: req.body.tel
// 		})
//				} else {
//
//				res.json({msg:'短信发送失败'})
//			}
//		})
	}
}

?>